Yacon.widget.Drop = function(conText)
{
	this.fieldType  = "";
	this.winUrl    = "";
	this.dropIco   = "";
	this.dropNorm  = false;
	this.isshow    = false;	
	this.isslide   = false;
	this.ischeck   = true;
	this.display   = false;
	Yacon.apply(this,conText);
	
	this.create();
}

Yacon.widget.Drop.Extend(Yacon.widget.WinControl,{	
	
	create:function()
	{
		if (this.attach)
			this.setAttach(this.attach);
					
		if (!this.element)
			this.element  = document.getElementById(this.elementID);	
		
		if (!this.element)
			return;	
		
		this.element.Control   = this;
		this.fieldName         = this.element.getAttribute("fieldName");		
		this.elementType       = "drop";
			
			
		if (this.element.tagName.toUpperCase() == "SELECT")
			this.selectDrop();		
		else
			this.elementDrop();	
		
		this.setDataSet();
		
		if (Yacon.isTrue(this.element.getAttribute("fieldDisabled")))
			this.setDisabled(true);	
		else
			this.setDisabled();
		
		this.getDropButton();
		
		this.analyseMaster();
   }, 
   
   selectDrop:function()
   {
	    this.addEvents("change",Yacon.widget.Drop.events.onchange);
	     	
		addElement(this.element);
   },   
   elementDrop:function()
   {
	   	this.element.setAttribute("readOnly",true);			
		this.element.className = "drop";
		  
		this.fieldType      = this.element.getAttribute("fieldType").trim();
		this.winUrl         = this.element.getAttribute("winUrl").trim();
		this.dropIco        = this.element.getAttribute("dropIco");
		this.ischeck    	= Yacon.isTrue(this.element.getAttribute("ischeck"));
		this.isNull    	 	= Yacon.isTrue(this.element.getAttribute("ISNull"));
		this.dropNorm    	= Yacon.isTrue(this.element.getAttribute("dropNorm"));
		
		this.addEvents("click",Yacon.widget.Drop.events.onclick);
		
		if (this.fieldType ==Yacon.widget.FIELD_DROP_LIST)
		{
			if (!Yacon.isEmpty(this.element.getAttribute("dropRefresh")))
			{
				this.addEvents("keyup",Yacon.widget.Drop.events.onkeyup);
				this.element.setAttribute("readOnly",false);
			}
		}
			//this.addEvents("mouseleave",Yacon.widget.Drop.events.onMouseLeave);
		
		
   },
   attachElement:function(cell)
   {
	   if (cell)	
		   this.setAttach(cell);
		
	   if(!this.attach)
			return;
	   
	    this.elementType       = "drop";
	    
	    this.fieldName      = this.attach.getAttribute("fieldName");		
		this.fieldType      = this.attach.getAttribute("fieldType").trim();
		this.isNull    	 	= Yacon.isTrue(this.attach.getAttribute("fieldNull"));
		this.dropNorm    	= false;
		this.ischeck        = false;
		
		this.attach.dropControl = this;
		
		Yacon.addEvents(this.attach,"mouseleave",Yacon.widget.Drop.events.onMouseLeave);
		
		this.show();
   },
   setDataSet:function(dataSet)
   {
	   if (!dataSet)
	   {
		   if (window.dataSet)
				this.dataSet = window.dataSet;
		   else
		      	this.dataSet = getDatasetByID();
		
		   	if (this.dataSet)
			{
		   		this.dataSet.regElement(this);
				addElement(this.element);
				
				if (this.dataSet.isEdit()||this.dataSet.isPast())
				{
					this.setPriFieldValue(this.dataSet.getFieldValue(this.fieldName));
				}else if (this.element.tagName.toUpperCase() == "SELECT")
				{
					this.setFieldValue();
				}
			}		   		
	   }else
		{
			this.dataSet = dataSet;
		}
   },   
   setDisabled:function(disabled)
   {
	   if (!Yacon.isEmpty(disabled))
		{
			this.element.disabled = disabled;
		}else
		{
			if (this.dataSet && this.dataSet.getRecord())
			{	
				if (this.dataSet.getRecord().getState().isEqual("insert"))
				{
					//兼容性处理
					if(window.isIE8 || window.isIE9){
						this.element.disabled = Yacon.isTrue(this.element.insertDisabled);
					}else{
						this.element.disabled = Yacon.isTrue(this.element.getAttribute("insertDisabled"));
					}
				}else if (this.dataSet.getRecord().getState().isEqual("edit"))
				{
					//兼容性处理
					if(window.isIE8 || window.isIE9){
						this.element.disabled = Yacon.isTrue(this.element.editDisabled);
					}else{
						this.element.disabled = Yacon.isTrue(this.element.getAttribute("editDisabled"));
					}		
				}else
				{
					this.element.disabled = true;
				}
			}
		}
	},
	analyseMaster:function()
	{
		if (this.element.getAttribute("masterDrop"))
		{
			this.masterDrop = new Yacon.util.Container();
			this.masterDrop.setJsonStr(this.element.getAttribute("masterDrop"));
			
			var parentElement = document.getElementById(this.masterDrop.getAttrValue("masterID"));
			if (parentElement && parentElement.Control)
				parentElement.Control.addDetailDrop(this);
			
		}
	},
	addDetailDrop:function(detailDrop)
	{
		if (!this.detailDrops)
			this.detailDrops = new Array();
		
		this.detailDrops.add(detailDrop);
	},	
	broadDetailDrop:function()
	{
		if (this.detailDrops)
		{
			for(var i=0;i<this.detailDrops.length;i++)
			{
				this.detailDrops[i].refreshDrop(this.dataSet);				
			}
		}	
	},
	refreshDrop:function(mainDataSet)
	{
		if (this.masterDrop)
		{
				var dataSet =  getDatasetByID("drop_select");
			   dataSet.setWhereField(this.masterDrop.getAttrValue("masterID"),mainDataSet.getFieldValue(this.masterDrop.getAttrValue("masterID"))); 
			   /**外部调用函数*/
				if (Yacon.isfireuserEvent(this.masterDrop.getAttrValue("viewBeforeRefresh")))
				{
					Yacon.fireUserEvent(this.masterDrop.getAttrValue("viewBeforeRefresh"),[dataSet]);
				}
			   
			   dataSet.switchMessage(this.masterDrop.getAttrValue("refreshMethod"));
			   
			   for ( var i= this.element.options.length -1;i >-1; i--)
			   {
			    	 this.element.options.remove(i);
			   }
			   var isdata =false;
			   while(dataSet.next())
			   {
				     var opt = document.createElement("OPTION");
				     for (var i=0;i<dataSet.getRecord().getFieldCount();i++)
				     {
				    	 var field =dataSet.getRecord().getField(i);
				    	
				    	 opt.setAttribute(field.getFieldName().toLowerCase(),field.getFieldValue());
				     }				     
				     this.element.add(opt);
				     
				     isdata =true;
				}
			   if (isdata)
			   {
				   this.setFieldValue();
			   }
			}
	},
   
   refreshData:function()
   {
	   if (!this.element && !this.attach)
		   return;
	   
	   var dataSet =  getDatasetByID("drop_select");
	   if (!this.element||Yacon.isEmpty(this.element.getAttribute("dropRefresh")))
	   {
		   dataSet.setActionName("app.portal.system.enums.RefreshData");
		   
		   if (this.attach)
			   dataSet.setWhereField("dropdownexp",this.attach.headCell.getDropdownExp());
		   else
			   dataSet.setWhereField("dropdownexp",this.element.getAttribute("dropdownexp")); 
		 
		   dataSet.switchMessage("refreshDrop");	   
	   }else
	   {
		   dataSet.addAttribute("dropKey",this.element.value); 
			 
		   dataSet.switchMessage(this.element.getAttribute("dropRefresh"));	
	   }
	   
	   this.options = eval(dataSet.getDataValue());   
   },
   getDropButton:function()
   {
		if (!this.element || this.element.tagName.toUpperCase() == "SELECT")
			return;
		
		if (!this.dropButton)
	   {
		   this.dropButton = document.createElement("span");
		   this.dropButton.Control = this;
		   this.dropButton.className = "dropbutton";
		   this.dropButton.setAttribute("id","drop_button_"+this.elementID);			
		   this.dropButton.style.display = "none";	  
		   
		   Yacon.addEvents(this.dropButton,"click",Yacon.widget.Drop.events.onclick);
		 	   
		   this.dropIcon = document.createElement("img");
		   this.dropIcon.className="button_img";	
		
		   this.dropIcon.src = this.getPath("public/images/skin_blue/widget/arrow.gif");
		   this.dropIcon.Control = this;		   
		   
		   this.dropButton.appendChild(this.dropIcon);
		   document.body.appendChild( this.dropButton);
		   
		   this.dropButton.disabled =this.element.disabled;
	   }
	   
	   this.dropButtonVisbile();	   
   },
   dropButtonVisbile:function()
   {
	    if (this.dropButton)
	   {   
		   var pos = this.getAbsPos();	
		   if (window.isIE ||window.isfirefox)
		    {
			    this.dropButton.style.paddingTop = (pos[3] - 12)/3;	   
			    this.dropButton.style.height     = pos[3]-5;
			    this.dropButton.style.width      = pos[3]-4;
			    this.dropButton.style.left    = pos[0] + pos[2]-pos[3];
				this.dropButton.style.top     = pos[1];
				this.dropButton.style.display    = "block";
			 }else
			 {
				 this.dropButton.style.paddingTop = (pos[3] - 12)/3;	   
				 this.dropButton.style.height     = pos[3]-6;
				 this.dropButton.style.width      = pos[3]-6;
				 this.dropButton.style.left    = pos[0] + pos[2]-pos[3]+4;
				 this.dropButton.style.top     = pos[1]+2;
				 this.dropButton.style.display    = "block";
				
			 }
		  
	   }
   },
   dropButtonHidden:function()
   {
	   if (this.dropButton)
		   this.dropButton.style.display    = "none"; 		   
   },
   
   show:function()
   {
	   this.getListContainer();  
	  
	   this.getDropButton();
	  
	   if(this.isshow)
	   {
		  if(this.isslide)
			 window.Droplist.slideHide();
		  else
			 window.Droplist.falshHide();
		  
		  this.isshow = false;
	   }else
	  {
		  window.activeElement = this;
		  
		  if(this.isslide) 
			 window.Droplist.slideShow();
		  else
			 window.Droplist.falshShow();
		  
		  this.isshow = true;
	  }
   },
   hidden:function()
   {
	  if(this.isslide)
		 window.Droplist.slideHide();
	  else
		 window.Droplist.falshHide();
	  
	  this.isshow = false;
	  
	  if (!this.dropNorm)
		  this.dropButtonHidden();
	 
   },
   getListContainer:function()
   {
	   
	    window.Droplist.setAttachControl(this);
	 
	    if (this.dataSet)
	    {
	    	fieldValue = this.dataSet.getFieldValue(this.fieldName);
	    	if (!Yacon.isEmpty(fieldValue))
	    	{
	    		window.Droplist.selectList = fieldValue.split(",");
	    	}
	    }
	    
	    var pos = this.getAbsPos();		    
	    switch (this.fieldType)
	    {
	    	case Yacon.widget.FIELD_DROP_SELECT:	
	    	case Yacon.widget.FIELD_DROP_LIST:
			{
				if (Yacon.isEmpty(this.options))
					this.refreshData();
				
				window.Droplist.setConText(
				{
					 top:pos[1]+pos[3]-1,
					 left:pos[0],
					 width:pos[2],
					 height:150,
					 ischeck:this.ischeck,
					 display:this.display,
					 zindex:10000
				});
				
				//this.options = eval(this.element.getAttribute("option"));
				window.Droplist.appenChildList(this.options);
				break;
			}
			case "win":
			{
				/**window.Droplist.setConText(
			    {
			    	top:pos[1]+pos[3]-1,
			    	left:pos[0],
			    	width:280,
			    	height:350,
			    	zindex:1000			    	
			    });
								
				window.Droplist.appenChildURl(this.winUrl);
				*/
						
				var dialogArg = "dialogwidth:300px; Dialogheight:400px;center:yes;help:no;resizable:no;scroll:no;status:no";
				var arg = new Object();
					//arg.dataSet = dataset;		
				var  dd = window.showModalDialog(this.winUrl,arg,dialogArg);
				//dataset.setWhereField("keydbtype",node.key);		
				
				//if (dd =="1")
					//dataset.switchMessage("keyRestore");
				
				 this.isshow = false;
				break;
			}
			case "tree":
			{
				
				break;
			}
			case Yacon.widget.FIELD_DROP_DATE:
			{
				window.Droplist.setConText(
				{
					top:pos[1]+pos[3]-1,
					left:pos[0],
					width:290,
					height:235,
					display:this.display,
					zindex:100000
				 });
				window.Calendar.setAttachControl(this);		
				window.Droplist.appenChild(window.Calendar.getSignHTML("date"));
				window.Calendar.show(fieldValue);
					
				break;
			}
			case Yacon.widget.FIELD_DROP_DATETIME:
			{
				window.Droplist.setConText(
				{
					top:pos[1]+pos[3]-1,
					left:pos[0],
					width:290,
					height:235,
					display:this.display,
					zindex:10000
				 });
				window.Calendar.setAttachControl(this);		
				window.Droplist.appenChild(window.Calendar.getSignHTML("datetime"));
				window.Calendar.show(fieldValue);
						
				break;
			}
			case Yacon.widget.FIELD_DROP_MONTH:
			{
				break;
			}
			case Yacon.widget.FIELD_DROP_TIME:
			{
				break;
			}
		}
	    return window.Droplist;
	   
   },
   setFieldValue:function()
   {
	   if (this.element && this.element.tagName.toUpperCase() == "SELECT")
	   {
		   if (this.element.selectedIndex >= 0)
		   {
			  if (this.dataSet)
				this.dataSet.setFieldValueText(this.fieldName,this.element.value,this.element.options[this.element.selectedIndex].text);
			  
			  this.broadDetailDrop();
		   }  
		   
	   }else{ 
		   var dropValue = "";	
		   switch (this.fieldType)
		   {
		   		case Yacon.widget.FIELD_DROP_SELECT:	
		   		case Yacon.widget.FIELD_DROP_LIST:
				{
					var text = "";
					for (var i=0; i<this.options.length;i++)
					{
						if (window.Droplist.selectList.indexOf(this.options[i].key)>-1)
						{
							if (text == "")
							{
								dropValue	= this.options[i].key; 
								text 		= this.options[i].value;
							}else
							{
								dropValue	= dropValue + "," + this.options[i].key; 
								text 		= text + "," + this.options[i].value;
							}
						}
					}
					if (this.dataSet)
						this.dataSet.setFieldValueText(this.fieldName,dropValue,text);
					
					if (this.attach && this.attach.getAttribute("dataSet"))
						getDatasetByID(this.attach.getAttribute("dataSet")).setFieldValueText(this.fieldName,dropValue,text);   
					break;
				}
				case "win":
				{
					break;
				}
				case "tree":
				{
					break;
				}
				case Yacon.widget.FIELD_DROP_DATE:
				{
					var fieldDate = window.Calendar.year+"-";
					if (window.Calendar.month+1 <10)
						fieldDate = fieldDate + "0" +(window.Calendar.month+1)+"-";
					else
						fieldDate = fieldDate +(window.Calendar.month+1)+"-";
					
					if (window.Calendar.day <10)
						fieldDate = fieldDate + "0" +window.Calendar.day;
					else
						fieldDate = fieldDate + window.Calendar.day;
						
					if (this.dataSet)
						this.dataSet.setFieldValueText(this.fieldName,fieldDate,fieldDate);
					break;
				}
				case Yacon.widget.FIELD_DROP_DATETIME:
				{
					var fieldDate = window.Calendar.year+"-";
					
					if (window.Calendar.month+1 <10)
						fieldDate = fieldDate + "0" +(window.Calendar.month+1)+"-";
					else
						fieldDate = fieldDate +(window.Calendar.month+1)+"-";
					
					if (window.Calendar.day <10)
						fieldDate = fieldDate + "0" +window.Calendar.day;
					else
						fieldDate = fieldDate + window.Calendar.day;
					
					fieldDate = fieldDate +" " + window.Calendar.hour +":"+ window.Calendar.minute +":00";
					if (this.dataSet)
						this.dataSet.setFieldValueText(this.fieldName,fieldDate,fieldDate);	
					break;
				}
				case Yacon.widget.FIELD_DROP_MONTH:
				{
					break;
				}
				case Yacon.widget.FIELD_DROP_TIME:
				{
					break;
				}
			}
	   }
   },
   setPriFieldValue:function(fieldValue,fieldText)
   {
	  
	   if (this.element && this.element.tagName.toUpperCase() == "SELECT")
	   {
		   if (!this.element.options || this.element.options.length ==0)
		   {  
			   var opt     = document.createElement("OPTION"); 
			   opt.text     = fieldValue;
			   opt.value    = fieldValue;
			  
			   this.element.add(opt);		   
		   }
		   this.element.value   = fieldValue;
		   this.broadDetailDrop();
	   }else{   
		   var dropValue = "";	
		   switch (this.fieldType)
		   {
		   		case Yacon.widget.FIELD_DROP_LIST:
				{
					
					if (this.element)
					{
						 this.element.fieldValue    = fieldValue;
						 this.element.value    = fieldText;
					}
					break;
				}
				case "win":
				{
					break;
				}
				case "tree":
				{
					break;
				}
				case Yacon.widget.FIELD_DROP_DATE:
				{
					if (this.element)
						 this.element.value    = fieldValue;
					break;
				}
				case Yacon.widget.FIELD_DROP_DATETIME:
				{
					 if (this.element)
						 this.element.value    = fieldValue;
					break;
				}
				case Yacon.widget.FIELD_DROP_MONTH:
				{
					break;
				}
				case Yacon.widget.FIELD_DROP_TIME:
				{
					break;
				}
			}
	   }
   },
   getFieldValue:function()
   {
		if (this.dataSet)
			return this.dataSet.getFieldValue(this.fieldName);
		else if (this.element)
			return this.element.value;
		else
			return "";
	},
	free:function()
	{
		if (this.dataSet)
			this.dataSet.unregElement(this.element);		
	}
});
/**
 * window 对象选择框
 * @return
 */
function drop_win_select(dropElement,dropValue,dropLabel)
{
	var dropElement = window.parent.document.all[dropElement];
	if (dropElement)
	{
		dropElement.setAttribute("dropValue",dropValue);
		dropElement.value = dropLabel;
		dropElement.Control.hidden();
	}
}


Yacon.widget.Drop.events = {	
	onclick:function()
	{
		var element   = window.event.srcElement;		
		element.Control.show();			
	},
	onkeyup:function()
	 {
		 var key     = event.charCode||event.keyCode;
		
		 if (key==9 || key==13|| key==37|| key==39)
			return;
		
		 var element   = event.srcElement;
		 element.Control.options="";
		 element.Control.hidden();
		 element.Control.show();
	 },
	onMouseLeave:function()
	{
		var element   = window.event.srcElement;
		if (element)
		{
			var pos = Yacon.getAbsPosition(element);
			if (pos[0]<event.x && event.x < pos[0]+ pos[2] && event.y > pos[1])
			{;}else
			{
				if (element.Control && element.Control.hidden)
					element.Control.hidden();
				if (element.dropControl&& element.dropControl.hidden)
					element.dropControl.hidden();
			}
		}
	},
	onchange:function()
	{
		var element = event.srcElement;
		
		element.Control.setFieldValue();
				
		
		/**外部调用函数*/
		if (Yacon.isfireuserEvent(element.id+"_onchange"))
		{
			Yacon.fireUserEvent(element.id+"_onchange",[element]);
		}
		
	}
}

